package com.example.pinkcat.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.MakeVideoPage;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.activity.api.entity.VideoInfoEntity;
import com.example.pinkcat.api.entity.ContributeEntity;
import com.example.pinkcat.api.entity.ContributeStaticMsg;
import com.example.pinkcat.api.entity.ContributeVideoEntity;
import com.example.pinkcat.api.entity.VideowaibuEntity;
import com.example.pinkcat.api.getData.getData_waibu;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.sql.RowId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import rx.internal.schedulers.NewThreadWorker;

public class Tougao_RecyclerViewAdapter extends RecyclerView.Adapter<Tougao_RecyclerViewAdapter.ViewHolder> {

    private Context context;
    private Spinner mDropSpinner;
    private boolean isFirst=true;

    List<ContributeEntity> contributeList=new ArrayList<>();

    List<ContributeVideoEntity> contributevideoList = new ArrayList<>();

    //用户视频信息
    VideoInfoEntity videoInfoEntity;


    int location  = 0;


    public void addData(List<ContributeEntity> contributeList){
        this.contributeList.addAll(contributeList);
        notifyDataSetChanged();
    }

    public void addVideoData( List<ContributeVideoEntity> contributevideoList){
        this.contributevideoList.addAll(contributevideoList);
        notifyDataSetChanged();
    }

    public void setVideoData(List<ContributeVideoEntity> contributevideoList){
        if (contributevideoList!=null)
        this.contributevideoList =contributevideoList;
        notifyDataSetChanged();
    }
    public List<ContributeVideoEntity> getData(){
        return this.contributevideoList;
    }

    //设置视频信息
    public void setvideoinfoData(VideoInfoEntity videoInfoEntity){
        this.videoInfoEntity=videoInfoEntity;
        notifyDataSetChanged();
    }


    public Tougao_RecyclerViewAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.layout_tougao_message, parent, false);

        } else {
            view = LayoutInflater.from(context).inflate(R.layout.layout_tougao_video, parent, false);
        }
        return new Tougao_RecyclerViewAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //用户投稿粉丝、视频播放、评论、点赞数
        if (position ==0){
            if (ContributeStaticMsg.fans_num == null){
                holder.tg_img_tv1.setText("0"); //粉丝数量
            }else {
                holder.tg_img_tv1.setText(ContributeStaticMsg.fans_num);
            }
            if (ContributeStaticMsg.video_play_num == null){
                holder.tg_img_tv2.setText("0");                  //视频播放量
            }else {
                holder.tg_img_tv2.setText(ContributeStaticMsg.video_play_num);
            }
            if (ContributeStaticMsg.comment_num == null){
                holder.tg_img_tv3.setText("0");//评论数量
            }else {
                holder.tg_img_tv3.setText(ContributeStaticMsg.comment_num);
            }
            if (ContributeStaticMsg.like_num == null){
                holder.tg_img_tv4.setText("0"); //点赞数量
            }else {
                holder.tg_img_tv4.setText(ContributeStaticMsg.like_num);
            }
        }else {
        //用户投稿视频
        Glide.with(context).load(Uri.parse(contributevideoList.get(position-1).getVideo_cover())).into(holder.roundImg); //视频封面
        holder.tv_video_1.setText(String.valueOf(contributevideoList.get(position-1).getVideo_name())); //视频标题
            if (contributevideoList.get(position-1).getTotalplay() == null){
                holder.tv2.setText("0"); //视频播放量
            }else {
                holder.tv2.setText(String.valueOf(contributevideoList.get(position - 1).getTotalplay())); //视频播放量
            }
            if (contributevideoList.get(position-1).getLike_num() == null){
                holder.tv3.setText("0");
            }else {
                holder.tv3.setText(String.valueOf(contributevideoList.get(position - 1).getLike_num())); //点赞数量
            }
            if (contributevideoList.get(position-1).getComment_count() == null){
                holder.tv4.setText("0"); //评论数量
            }else {
                holder.tv4.setText(String.valueOf(contributevideoList.get(position - 1).getComment_count())); //评论数量
            }
//        holder.tv5.setText(""); //审核状态
//            getData_waibu.getContributeVideoMsg(context,Tougao_RecyclerViewAdapter.this,contributevideoList.get(position-1).getVideo_id());
        }


        if (isFirst)
            Spinner(holder);

        if (position == 0){
        holder.tgy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MakeVideoPage.class);
                context.startActivity(intent);
            }
        });
        } else {

            holder.tg_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, MakeVideoPage.class);
                    intent.putExtra("v_id",contributevideoList.get(position-1).getVideo_id());  //视频id
                    intent.putExtra("v_link",contributevideoList.get(position-1).getVideo_cover()); //视频封面
//                    intent.putExtra("v_intro",contributevideoList.get(position-1).getvideo); //视频简介
                    context.startActivity(intent);
                }
            });

            //用户投稿视频

        holder.tg_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(context,VideoActivity.class);
                intent.putExtra("v_id",contributevideoList.get(position-1).getVideo_id());
                intent.putExtra("up_id",userStaticMsg.userId);
                intent.putExtra("v_link",contributevideoList.get(position-1).getVideo_link());
                intent.putExtra("v_labs",contributevideoList.get(position-1).getVideo_lab());
                context.startActivity(intent);
            }
        });

            //删除
            holder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //删除没动画
//                    contributevideoList.remove(position-1);
//                    notifyDataSetChanged();
                    //删除有动画
//                    contributevideoList.remove(holder.getAdapterPosition()-1);
//                    notifyItemRemoved(holder.getAdapterPosition());
                    //对话框
                    AlertDialog.Builder builder=new AlertDialog.Builder(context);
                            builder.setTitle("Pink cat");
                            builder.setMessage("  你是否要删除该视频:");
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(context,"取消",Toast.LENGTH_SHORT).show();
                                }
                            });
                            builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(context,"删除完成",Toast.LENGTH_SHORT).show();
                                    contributevideoList.remove(holder.getAdapterPosition()-1);
                                    notifyItemRemoved(holder.getAdapterPosition());
                                    notifyDataSetChanged();
                                }
                            });

                            AlertDialog b = builder.create();
                            b.show();
                }
            });

        }




    }


    @Override
    public int getItemCount() {
        return contributevideoList.size()+1;
    }

    //返回数据类型
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RoundedImageView roundImg;
        private TextView tv_video_1;

        private TextView tv2;
        private TextView tv3;
        private TextView tv4;
        private TextView tv5;

        private TextView tg_img_tv1;
        private TextView tg_img_tv2;
        private TextView tg_img_tv3;
        private TextView tg_img_tv4;

        private EditText tg_ed;

        private Spinner mSpinner; //下拉框

        private TextView tgy;
        private View tg_video;

        private TextView tg_edit;

        private View del;
        


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            roundImg = itemView.findViewById(R.id.tougao_img_video_1);

            tv_video_1 = itemView.findViewById(R.id.tougao_tv_video_1);
            tv2 = itemView.findViewById(R.id.tougao_tv_video_2);
            tv3 = itemView.findViewById(R.id.tougao_tv_video_3);
            tv4 = itemView.findViewById(R.id.tougao_tv_video_4);
            tv5 = itemView.findViewById(R.id.tougao_tv_video_5);



            tg_img_tv1 = itemView.findViewById(R.id.tougao_img_1);
            tg_img_tv2 = itemView.findViewById(R.id.tougao_img_2);
            tg_img_tv3 = itemView.findViewById(R.id.tougao_img_3);
            tg_img_tv4 = itemView.findViewById(R.id.tougao_img_4);

            tg_ed = itemView.findViewById(R.id.tougao_ed_1);

            mSpinner = itemView.findViewById(R.id.tougao_spinner_1);

            tgy = itemView.findViewById(R.id.tougao_tv_1);
            tg_video = itemView.findViewById(R.id.tougao_rv_video_1);

            tg_edit = itemView.findViewById(R.id.tougao_tv_video_edit);

            del = itemView.findViewById(R.id.tougao_tv_video_del);
        }
    }


    private void Spinner(ViewHolder holder){
        isFirst = false;
        if (holder.mSpinner==null)
            return;

//        String[] list_arr = new String[]{"按日期排序","按播放量排序"};
//
//        ArrayList<String> adapter = new ArrayList<String>(,R.layout.,list_arr);
        List<String> list = new ArrayList<String>();
            list.add("最新投稿");
            list.add("最多播放");
            list.add("最多点赞");
        ArrayAdapter<String> spinner_adapter=new ArrayAdapter<>(context, android.R.layout.simple_spinner_item,list);
        spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        holder.mSpinner.setAdapter(spinner_adapter);
        holder.mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(context,"你想点"+position,Toast.LENGTH_SHORT).show();
                location = position;
                order(location);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void order( int order) {
        Collections.sort(contributevideoList, new Comparator<ContributeVideoEntity>() {
            @Override
            public int compare(ContributeVideoEntity o1, ContributeVideoEntity o2) {
                if (order == 0) {
                    return o2.getVideo_pub_time().compareTo(o1.getVideo_pub_time());
                } else if (order == 1) {
                    if (o1.getTotalplay()==null)
                        return 1;
                    if (o2.getTotalplay()==null)
                        return -1;
                    int o1_play = Integer.parseInt(o1.getTotalplay());
                    int o2_play = Integer.parseInt(o2.getTotalplay());
                    if (o1_play<o2_play) return 1;
                    else return -1;
                } else if (order == 2) {
                    if (o1.getLike_num()==null)
                        return 1;
                    if (o2.getLike_num() ==null)
                        return -1;
                    int o1_like = Integer.parseInt(o1.getLike_num());
                    int o2_like  = Integer.parseInt(o2.getLike_num());
                    if (o1_like < o2_like) return 1;
                    else return -1;
                }

                    return 0;
            }
        });
        Tougao_RecyclerViewAdapter.this.setVideoData(contributevideoList);
    }



}
